package com.tools.demo.ocr;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import com.tools.demo.pbTools.Base64Tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: Tools_Demo
 * @description: 发票识别
 * @author: xujiaxin
 * @create: 2023-02-06 15:48
 */
public class InvoiceOcr {

    /**
     * 获取文件内容
     * @param file
     * @return
     */
    public static String txt2String(File file) {
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file)); //构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) { //使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();

    }

    public static void main(String[] args) {
        // 获取base64
        String img = "D:\\workspace\\Tools_Demo\\static\\发票.jpg";
//        System.out.println(imageToBase64Str(img));

        // 获取base64
        File file = new File("D:\\workspace\\Tools_Demo\\static\\fapiao.txt");
        String imgBase64 = Base64Tools.imageToBase64Str(img);

        Map<String, Object> job = new HashMap();
        job.put("method", "ocrStructuredService"); // 固定参数
        job.put("img", imgBase64);
        job.put("type", "invoice"); //结构化图片的类型，括号内表示需要传入的参数值，增值税发票(invoice)

        String res = HttpUtil.createRequest(Method.POST, "http://172.18.49.24/ocrapidocker/ocrservice.json")
                .contentType("application/x-www-form-urlencoded;charset=UTF-8")
                .form(job)
                .execute()
                .body();
        System.out.println(res);

    }

}
